﻿using Juli.Mes.Stoves.Dtos;
using Juli.Mes.Vehicles.Dtos;
using Lion.AbpPro.Core;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.ObjectMapping;

namespace Juli.Mes.Vehicles
{
    [Route("Vehicles")]
    [Authorize]
    public class VehicleAppService : MesAppService, IVehicleAppService
    {
        private readonly IRepository<Vehicle> vehicleRepository;
        private readonly IRepository<IdentityUser> userRepository;

        public VehicleAppService(IRepository<Vehicle> vehicleRepository,
             IRepository<IdentityUser> userRepository)
        {
            this.vehicleRepository = vehicleRepository;
            this.userRepository = userRepository;
        }

        [HttpPost("create")]
        [SwaggerOperation("新增车辆", Tags = new[] { "Vehicles" })]
        public async Task CreateVehicle(VehicleInputDto input)
        {
            var entity = ObjectMapper.Map<VehicleInputDto, Vehicle>(input);
            await vehicleRepository.InsertAsync(entity);
        }

        [HttpPost("delete")]
        [SwaggerOperation("删除车辆", Tags = new[] { "Vehicles" })]
        public async Task DeleteVehicle(IdInput input)
        {
            await vehicleRepository.DeleteAsync(p => p.Id == input.Id);
        }

        [HttpPost("edit")]
        [SwaggerOperation("编辑车辆", Tags = new[] { "Vehicles" })]
        public async Task EditVehicle(VehicleInputDto input)
        {
            var entity = await vehicleRepository.GetAsync(p => p.Id == input.Id);
            ObjectMapper.Map(input, entity);
            await vehicleRepository.UpdateAsync(entity);
        }

        [HttpPost("page")]
        [SwaggerOperation("分页查询车辆", Tags = new[] { "Vehicles" })]
        public async Task<PagedResultDto<VehicleOutputDto>> ListVehicle(PageVehicleInputDto input)
        {
            var vehicleQuery = await vehicleRepository.GetQueryableAsync();
            var userQuery = await userRepository.GetQueryableAsync();

            var query = from s in vehicleQuery
                        join u1 in userQuery on s.CreatorId equals u1.Id
                        join u2 in userQuery on s.LastModifierId equals u2.Id into u22
                        from u23 in u22.DefaultIfEmpty()
                        select new VehicleOutputDto()
                        {
                            Id = s.Id,
                            VehicleCode = s.VehicleCode,
                            VehicleName = s.VehicleName,
                            Description = s.Description,
                            Rfid = s.Rfid,
                            State = s.State,
                            Type = s.Type,
                            Active = s.Active,
                            CreationTime = s.CreationTime,
                            LastModificationTime = s.LastModificationTime,
                            CreatorName = u1.Name,
                            LastModifierName = u23.Name
                        };

            query = query.WhereIf(!string.IsNullOrEmpty(input.Filter),
                p => p.VehicleCode.Contains(input.Filter)
                || p.VehicleName.Contains(input.Filter)
                || p.Rfid.Contains(input.Filter));

            var items = query
                .OrderBy(e => e.CreationTime)
                .PageBy(input.SkipCount, input.PageSize)
                .ToList();

            var count = query.Count();

            return new PagedResultDto<VehicleOutputDto>()
            {
                Items = items,
                TotalCount = count,
            };
        }
    }
}
